$(document).ready(function () {

    $("#saveTaskButton").click(function () {

        var task_taskName = $("#task_taskName").val();
        if ($.trim(task_taskName) == "") {
            alert("“任务名称”不能为空");
            return;
        }

        var task_webServiceUrl = $("#task_webServiceUrl").val();
        if ($.trim(task_webServiceUrl) == "") {
            alert("“地址”不能为空");
            return;
        }

        var task_owner = $("#task_owner").val();
        if ($.trim(task_owner) == "") {
            alert("“环境”不能为空");
            return;
        }

        var task_cycle = $("#task_cycle").val();
        if (task_cycle == 'WEEK') {
            var count = $("input[name='weeks']:checkbox:checked").size();
            if (count == 0) {
                alert("请选择周几执行");
                return;
            }
            $("#task_cycleDimension").val("1");
        }

        var task_planTime = $("#task_planTime").val();
        if (task_cycle != 'CRON' && $.trim(task_planTime) == "") {
            alert("“计划时间”不能为空");
            return;
        }

        var cycleDimension = $("#task_cycleDimension");
        var cycleDimensionVal = $.trim($(cycleDimension).val());
        var type = "^[0-9]*[1-9][0-9]*$";
        var re = new RegExp(type);
        if (cycleDimensionVal.match(re) == null) {
            alert("“周期尺度”应为正整数!");
            $(cycleDimension).val("1");
            $(cycleDimension).focus();
            return;
        }

        var task_soTimeout =$("#task_soTimeout");
        if ($.trim(task_soTimeout.val()) != "" && $.trim(task_soTimeout.val()).match(re) == null) {
            alert("“超时时间”应为正整数!");
            $(task_soTimeout).val("");
            $(task_soTimeout).focus();
            return;
        }

        var cycle = $("#task_cycle").val();
        if (cycle == 'CRON') {
            $.post(
                basePath + "pub/testCron.do",
                $("#task_form").serialize(),
                function (data) {
                    if (data.jsonMap.status == "success") {
                        saveSubmit();
                    } else {
                        alert(data.jsonMap.status);
                    }
                }
            );

        } else {
            saveSubmit();
        }
    });

    $("#task_cycle").change(function () {
        $("#cron").hide();
        $("#week").hide();
        $("#dimension").hide();
        $("#planTime").hide();

        if (this.value == 'CRON') {
            $("#cron").show();
        }else if (this.value == 'WEEK') {
            $("#week").show();
            $("#planTime").show();
        } else {
            $("#dimension").show();
            $("#planTime").show();
        }
    }).change();

    $("#testCron").click(function(){
        $.post(
            basePath + "pub/testCron.do",
            $("#task_form").serialize(),
            function (data) {
                if (data.jsonMap.status == "success") {
                    alert(data.jsonMap.info);
                } else {
                    alert(data.jsonMap.status);
                }
            }
        );
    });
    $("#showBook").click(function () {
        if ($("#bookInfo").is(":visible")) {
            $("#bookInfo").hide();
        } else {
            $("#bookInfo").show();
        }
    });
});

function saveSubmit(){
    $.post(
        basePath + "pub/saveTask.do",
        $("#task_form").serialize(),
        function (data) {

            if (data.jsonMap.status == "success") {
                alert("保存任务成功");
                window.parent.refreshWindow()
            } else {
                alert(data.jsonMap.status);
            }
        }
    );
}